package cn.imvc.app.controllers.goods;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.entities.TypeClass;
import cn.imvc.app.entities.TypeValue;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.TypeClassModel;
import cn.imvc.app.models.TypeValueModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

/**
 * 
 * @author zhanghy
 * @date 2016年8月11日上午11:11:19
 *
 */
public class TypeValueControllerImpl implements TypeValueController{
	static Logger logger = Logger.getLogger(TypeValueControllerImpl.class);
	public Page<TypeValue> page;
	public ViewRequest viewRequest;
	public TypeValue typeValue;
	public TypeClass typeClass;
	public int id;
	public int[] ids;
	public int typeClassId;
	public String search_content;
	
	@Autowired
	TypeValueModel typeValueModel;
	@Autowired
	TypeClassModel typeClassModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		return null;
	}

	public String index() {
		Map<String,String> searchMap = new HashMap<String,String>();
		searchMap.put("typeClassId_eq_string", String.valueOf(typeClassId));
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.setParams(searchMap);
		viewRequest.getSort().put("sort", "ASC");
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
			viewRequest.getParams().put("_and_typeClass_dot_id_eq_number", ""+typeClassId);
		}
		page = typeValueModel.find(viewRequest);
		return "type-value/index";
	}

	public String show() {
		return null;
	}

	public String editNew() {
		typeClass = typeClassModel.get(typeClassId);
		return "type-value/edit";
	}

	public String create() {
		typeValue.setTypeClass(typeClassModel.get(typeClassId));
		int result = typeValueModel.save(typeClass,typeValue);
		if(result==0){
			return Render.json(new DialogBean("300","类型值编码不能重复","type-value","closeCurrent"));
		}else if(result==-1){
			return Render.json(new DialogBean("300","类型值名称不能重复","type-value","closeCurrent"));
		}
		loggingModel.info(logger,"添加类型值："+typeValue.getName());
		return Render.json(new DialogBean("200","添加成功","type-value","closeCurrent"));
	}

	public String edit() {
		
		typeValue = typeValueModel.get(id);
		typeClass = typeValue.getTypeClass();
		return "type-value/edit";
	}

	public String update() {
		typeValue.setTypeClass(typeClassModel.get(typeClassId));
		int result = typeValueModel.update(id,typeClass,typeValue);
		if(result==0){
			return Render.json(new DialogBean("300","类型值编码不能重复","type-value","closeCurrent"));
		}else if(result==-1){
			return Render.json(new DialogBean("300","类型值名称不能重复","type-value","closeCurrent"));
		}
		loggingModel.info(logger,"修改类型值："+typeValue.getName());
		return Render.json(new DialogBean("200","修改成功","type-value","closeCurrent"));
	}

	public String destroy() {
		String result = typeValueModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除类型值："+id);
		    return Render.json(new DialogBean("200","删除成功","type-value"));
		}else{
			return Render.json(new DialogBean("300",result,"type-value"));
		}
	}

	public String destroyBatch() {
		typeValueModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功","type-value"));
	}
/**
 * 商品调用
 * 
 */
	public String dialog() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content!=null&&!search_content.equals("")){
		
			viewRequest.getParams().put("name_like", search_content);
		}
		if(typeClassId != 0){
			viewRequest.getParams().put("_and_typeClass_eq_number",""+typeClassId );
		}
		page = typeValueModel.find(viewRequest);
		return "type-value/dialog";
	}
	/**
	 * 首页调用
	 *
	 */
	public String dialog2() {
		Map<String,String> searchMap = new HashMap<String,String>();
		searchMap.put("typeClassId_eq_string", String.valueOf(typeClassId));
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.setParams(searchMap);
		viewRequest.getSort().put("sort", "ASC");
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
			viewRequest.getParams().put("_and_typeClass_dot_id_eq_number", ""+typeClassId);
		}
		page = typeValueModel.find(viewRequest);
		return "type-value/dialog2";
	}
	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public void setTypeClass(TypeClass typeClass) {
		this.typeClass = typeClass;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}

	public void setTypeValueModel(TypeValueModel typeValueModel) {
		this.typeValueModel = typeValueModel;
	}

	public void setTypeValue(TypeValue typeValue) {
		this.typeValue = typeValue;
	}

	public void setTypeClassId(int typeClassId) {
		this.typeClassId = typeClassId;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public TypeValue getTypeValue() {
		return typeValue;
	}

	public TypeClass getTypeClass() {
		return typeClass;
	}

	public int getId() {
		return id;
	}

	public int[] getIds() {
		return ids;
	}

	public int getTypeClassId() {
		return typeClassId;
	}

	public String getSearch_content() {
		return search_content;
	}


}
